package com.woniu.woniuk16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.woniuk16.aop.Mes;
import com.woniu.woniuk16.commens.ComUtil;
import com.woniu.woniuk16.dto.ClassUserDto;
import com.woniu.woniuk16.dto.ClassUsersDto;
import com.woniu.woniuk16.dto.WoniuUserDetails;
import com.woniu.woniuk16.entity.WoniuClass;
import com.woniu.woniuk16.entity.WoniuClassUser;
import com.woniu.woniuk16.entity.WoniuCource;
import com.woniu.woniuk16.entity.WoniuUser;
import com.woniu.woniuk16.exception.stu.CourceExceprion;
import com.woniu.woniuk16.mapper.WoniuClassUserMapper;
import com.woniu.woniuk16.param.GetByClass;
import com.woniu.woniuk16.param.PlanCourceParam;
import com.woniu.woniuk16.service.WoniuClassService;
import com.woniu.woniuk16.service.WoniuClassUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.woniuk16.service.WoniuCourceService;
import com.woniu.woniuk16.service.WoniuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-16
 */
@Service
public class WoniuClassUserServiceImpl extends ServiceImpl<WoniuClassUserMapper, WoniuClassUser> implements WoniuClassUserService {
    @Autowired
    private WoniuClassService woniuClassService;
    @Autowired
    private WoniuUserService woniuUserService;
    @Autowired
    private WoniuCourceService woniuCourceService;

    @Mes
    @Override
    public boolean planCource(PlanCourceParam planCourceParam) throws ParseException, CourceExceprion {
        String start =  planCourceParam.getCourceStarttime().split("T")[0];
        String end = planCourceParam.getCourceEndtime().split("T")[0];
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(start);
        Date parse1 = simpleDateFormat.parse(end);
        if (parse.getTime()>parse1.getTime()) {
            throw new CourceExceprion("开始与结束时间有误");
        }
        if (planCourceParam.getClassId()==null){
            throw new CourceExceprion("必须选择班级");
        }
        if (planCourceParam.getCourceId()==null){
            throw new CourceExceprion("必须选择课程");
        }

        QueryWrapper<WoniuClassUser> qw = new QueryWrapper();
        qw.eq("class_id",planCourceParam.getClassId());
        List<WoniuClassUser> list = list(qw);
        for (WoniuClassUser woniuClassUser : list) {
            if (woniuClassUser.getCourceId()==planCourceParam.getCourceId()){
                throw new CourceExceprion("系统已存在该课程");
            }
        }


        WoniuUserDetails details = (WoniuUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        WoniuClassUser woniuClassUser = ComUtil.copyProperties(planCourceParam, WoniuClassUser.class);
        woniuClassUser.setUserId(details.getUserId());
        woniuClassUser.setClassUserStatus(0);
        woniuClassUser.setCourceStarttime(start);
        woniuClassUser.setCourceEndtime(end);
        save(woniuClassUser);
        return true;
    }

    @Override
    public List<ClassUserDto> getcuds() {
        List<WoniuClassUser> list = list();
        if (list==null||list.size()<=0){
            return null;
        }
        List<ClassUserDto> classUserDtos = ComUtil.copyList(list, ClassUserDto.class);

        List<Integer> collect = list.stream().map(WoniuClassUser::getClassId).collect(Collectors.toList());
        QueryWrapper<WoniuClass> woniuClassQueryWrapper = new QueryWrapper<>();
        woniuClassQueryWrapper.in("class_id",collect);
        List<WoniuClass> list1 = woniuClassService.list(woniuClassQueryWrapper);
        for (int i = 0; i < classUserDtos.size(); i++) {
            for (WoniuClass woniuClass : list1) {
                ComUtil.listAddByField(classUserDtos.get(i),woniuClass,"classId",Integer.class);
            }
        }

        List<Integer> collect2 = list.stream().map(WoniuClassUser::getUserId).collect(Collectors.toList());
        QueryWrapper<WoniuUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("user_id",collect2);
        List<WoniuUser> list2 = woniuUserService.list(userQueryWrapper);
        ComUtil.listAddByFields(classUserDtos,list2,"userId",Integer.class);

        List<Integer> collect3 = list.stream().map(WoniuClassUser::getCourceId).collect(Collectors.toList());
        QueryWrapper<WoniuCource> wcq = new QueryWrapper<>();
        wcq.in("cource_id",collect3);
        List<WoniuCource> list3 = woniuCourceService.list(wcq);
        ComUtil.listAddByFields(classUserDtos,list3,"courceId",Integer.class);


        return classUserDtos;
    }

    @Override
    public ClassUsersDto getcudsByClass(GetByClass byClass) throws CourceExceprion {

        QueryWrapper<WoniuClassUser> qw = new QueryWrapper<>();
        if (byClass.getClassId()!=null){
            qw.eq("class_id",byClass.getClassId());
        }

        Page<WoniuClassUser> p = new Page<>();
        if (byClass.getPageIndex()==null){
            byClass.setPageIndex(1l);
        }
        p.setCurrent(byClass.getPageIndex());
        p.setSize(byClass.getPageSize());
        Page<WoniuClassUser> page1 = page(p, qw);
        ClassUsersDto classUsersDto = new ClassUsersDto();
        List<WoniuClassUser> list = page1.getRecords();
        if (list==null||list.size()<=0){
            throw new CourceExceprion("没有数据");
        }

        List<ClassUserDto> classUserDtos = ComUtil.copyList(list, ClassUserDto.class);
        List<Integer> collect = list.stream().map(WoniuClassUser::getClassId).collect(Collectors.toList());
        QueryWrapper<WoniuClass> woniuClassQueryWrapper = new QueryWrapper<>();
        woniuClassQueryWrapper.in("class_id",collect);
        List<WoniuClass> list1 = woniuClassService.list(woniuClassQueryWrapper);
        for (int i = 0; i < classUserDtos.size(); i++) {
            for (WoniuClass woniuClass : list1) {
                ComUtil.listAddByField(classUserDtos.get(i),woniuClass,"classId",Integer.class);
            }
        }

        List<Integer> collect2 = list.stream().map(WoniuClassUser::getUserId).collect(Collectors.toList());
        QueryWrapper<WoniuUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("user_id",collect2);
        List<WoniuUser> list2 = woniuUserService.list(userQueryWrapper);
        ComUtil.listAddByFields(classUserDtos,list2,"userId",Integer.class);

        List<Integer> collect3 = list.stream().map(WoniuClassUser::getCourceId).collect(Collectors.toList());
        QueryWrapper<WoniuCource> wcq = new QueryWrapper<>();
        wcq.in("cource_id",collect3);
        List<WoniuCource> list3 = woniuCourceService.list(wcq);
        ComUtil.listAddByFields(classUserDtos,list3,"courceId",Integer.class);





        classUsersDto.setAccountDtos(classUserDtos);
        classUsersDto.setPageIndex(page1.getCurrent());
        classUsersDto.setTotal(page1.getTotal());
        return classUsersDto;
    }


}
